﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Box2D.XNA;

namespace AAEngine.Box2D
{
    public class ContactListener : IContactListener
    {
        public void BeginContact(Contact contact)
        {
            Box2DSpatialComponent spatial1 = contact.GetFixtureA().GetUserData() as Box2DSpatialComponent;
            Box2DSpatialComponent spatial2 = contact.GetFixtureB().GetUserData() as Box2DSpatialComponent;

            if (spatial1.Owner != null)
            {
                spatial1.Owner.EventDispatcher.DispatchEvent(new CollisionEvent(CollisionEvent.COLLISION_EVENT, contact));
            }

            if (spatial2.Owner != null)
            {
                spatial2.Owner.EventDispatcher.DispatchEvent(new CollisionEvent(CollisionEvent.COLLISION_EVENT, contact));
            }
        }

        public void EndContact(Contact contact) 
        {
            Box2DSpatialComponent spatial1 = contact.GetFixtureA().GetUserData() as Box2DSpatialComponent;
            Box2DSpatialComponent spatial2 = contact.GetFixtureB().GetUserData() as Box2DSpatialComponent;

            if (spatial1.Owner != null)
            {
                spatial1.Owner.EventDispatcher.DispatchEvent(new CollisionEvent(CollisionEvent.COLLISION_STOPPED_EVENT, contact));
            }

            if (spatial2.Owner != null)
            {
                spatial2.Owner.EventDispatcher.DispatchEvent(new CollisionEvent(CollisionEvent.COLLISION_STOPPED_EVENT, contact));
            }
        }


        public void PreSolve(Contact contact, ref Manifold oldManifold)
        {
        }

        public void PostSolve(Contact contact, ref ContactImpulse impulse)
        {
        }
    }
}
